package com.baox.agridata.synchronous.assembly;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baox.agridata.synchronous.constant.Constant;
import com.baox.agridata.synchronous.entity.quanguo.Jyzsdwb;
import com.baox.agridata.synchronous.entity.shanxi.TCCOutAAEntity;
import com.baox.agridata.synchronous.entity.shanxi.TXDRSlaughterHouse;
import com.baox.agridata.synchronous.service.shanxi.TBaseRegionService;
import com.baox.agridata.synchronous.service.shanxi.TXDRSlaughterHouseService;
import com.baox.agridata.synchronous.util.DateUtil;
import com.baox.agridata.synchronous.util.DicDataTurnUtil;
import com.baox.agridata.synchronous.util.MapUtil;
import com.baox.agridata.synchronous.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.util.Map;

/**
 * 全国到达本省的动物A数据
 */
@Component
@Slf4j
public class TCCOutAAAssembly {

    @Autowired
    private TBaseRegionService tBaseRegionService;

    @Autowired
    private TXDRSlaughterHouseService txdrSlaughterHouseService;

    /**
     * 全国数据转换成本地的TCCOutAA表中的数据
     *
     * @return
     */
    public TCCOutAAEntity JyzqgdwaToOutAnimalA(Jyzsdwb oldEntity) {
        TCCOutAAEntity newEntity = null;
        try {
            if (oldEntity != null) {
                newEntity = new TCCOutAAEntity();
                String UUID = oldEntity.getId();
                newEntity.setMid(UUID.replace("-",""));            //mid
                newEntity.setSource_id(null);                   //source_id
                newEntity.setName(null);                        //name
                newEntity.setOld_id(null);                      //old_id
                newEntity.setCCType(Constant.ANIMAL_A);         //CCType
                newEntity.setQRCode(oldEntity.getEwm());        //QRCode
                newEntity.setFactoryCode(oldEntity.getZbh());   //FactoryCode
                newEntity.setOwner(oldEntity.getHz());          //Owner
                newEntity.setOwnerTel(oldEntity.getHzdh());     //OwnerTel
                newEntity.setAmount(oldEntity.getSl());         //Amount
                String l_RegionCode = oldEntity.getQqhdm() + "000000";
                Map startRegion = tBaseRegionService.selectRegionInfoByRegionCodeForAnimalAndProduct(l_RegionCode);
                if (startRegion != null) {
                    String l_RegionMap = JSON.toJSONString(startRegion);
                    newEntity.setStartingPlaceRegion(l_RegionMap);       //StartingPlaceRegion
                }
                newEntity.setStartingPlaceName(oldEntity.getQdmc());      //StartingPlaceName
                String l_desRegionCode = oldEntity.getDqhdm() + "000000";
                Map desRegion = tBaseRegionService.selectRegionInfoByRegionCodeForAnimalAndProduct(l_desRegionCode);
                if (desRegion != null) {
                    String l_RegionMap = JSON.toJSONString(desRegion);
                    newEntity.setDestinationPlaceRegion(l_RegionMap);                  //DestinationPlaceRegion
                    newEntity.setDestinationPlaceTownName(MapUtil.getString(desRegion,"RegionName"));  //DestinationPlaceTownName
                    newEntity.setDestinationPlaceRegionID(MapUtil.getInteger(desRegion,"ID"));
                    newEntity.setDestinationPlaceRegionRI1(MapUtil.getInteger(desRegion,"RI1"));
                    newEntity.setDestinationPlaceRegionRI2(MapUtil.getInteger(desRegion,"RI2"));
                    newEntity.setDestinationPlaceRegionRI3(MapUtil.getInteger(desRegion,"RI3"));
                    newEntity.setDestinationPlaceRegionRI4(MapUtil.getInteger(desRegion,"RI4"));
                    newEntity.setDestinationPlaceRegionRI5(MapUtil.getInteger(desRegion,"RI5"));
                }
                newEntity.setDestinationPlaceName(oldEntity.getDdmc());                  //DestinationPlaceName
                newEntity.setCarrier(oldEntity.getYsmc());                               //Carrier
                newEntity.setCarrierTel(oldEntity.getYsdh());                            //CarrierTel
                newEntity.setTransportNumber(oldEntity.getYsph());                       //TransportNumber
                newEntity.setTransportRecordNo(null);                                    //TransportRecordNo
                newEntity.setDisinfectType(oldEntity.getYsxd());                         //DisinfectType
                newEntity.setAgengcyName(oldEntity.getQfdw());                           //AgengcyName
                newEntity.setQuarantineOfficerName(oldEntity.getGfsyxm());               //QuarantineOfficerName
                newEntity.setDateOfIssue(DateUtil.dateFormat.format(oldEntity.getRq()));  //DateOfIssue
                newEntity.setEarTags(oldEntity.getDebh());                                //EarTags
                newEntity.setRemark(oldEntity.getBz());                                   //Remark
                newEntity.setAgencyTel(oldEntity.getQfdh());                              //AgencyTel
                newEntity.set_PartId("d5896b31964e425382df52f655dedfc2");                 //_PartId
                newEntity.setUUID(UUID);                                                    //UUID
                newEntity.setCheckNormalCount(null); //实际数量
                newEntity.setCheckAbnormalCount(null); //异常数量
                newEntity.setCheckDeadCount(null); //死亡数量
                newEntity.setCheckRemark(null); //备注信息
                newEntity.setCheckUserMid(null); //检查人员
                newEntity.setCheckTime(null); //检查时间

                JSONObject _Status = new JSONObject();
                _Status.put("key", 1703);
                _Status.put("Name", "未回收");
                newEntity.setStatus(_Status.toJSONString());                                //Status

                JSONObject _CCOutInType = new JSONObject();
                _CCOutInType.put("key", 1901);
                _CCOutInType.put("Name", "入省");
                newEntity.setCCOutInType(_CCOutInType.toJSONString());                      //CCOutInType

                JSONObject _PrintStatus = new JSONObject();
                _PrintStatus.put("key", 1801);
                _PrintStatus.put("Name", "已打印");
                newEntity.setPrintStatus(_PrintStatus.toJSONString());                      //_PrintStatus

                JSONObject _IsOnlineIssuing = new JSONObject();
                _IsOnlineIssuing.put("key", 2301);
                _IsOnlineIssuing.put("Name", "在线");
                newEntity.setIsOnlineIssuing(_IsOnlineIssuing.toJSONString());              //_IsOnlineIssuing

                Map validityMap = DicDataTurnUtil.ValidityDaysTurnMap(oldEntity.getZddyxq());
                if (validityMap != null) {
                    newEntity.setValidityDays(JSON.toJSONString(validityMap));              //ValidityDays
                }

                if (StringUtil.isNotEmpty(oldEntity.getYsfs())) {
                    newEntity.setTransportType(JSON.toJSONString(DicDataTurnUtil.TransportTypeTurnMap(oldEntity.getYsfs())));  //TransportType
                }

                if (StringUtil.isNotEmpty(oldEntity.getDytdm())) {
                    newEntity.setUsageType(JSON.toJSONString(DicDataTurnUtil.PurposeTurnMap(oldEntity.getDytdm())));      //usageType
                }

                if (StringUtil.isNotEmpty(oldEntity.getDlbdm())) {
                    newEntity.setDestinationPlaceType(JSON.toJSONString(DicDataTurnUtil.PlaceTypeTurnMap(oldEntity.getDlbdm())));  //DestinationPlaceType
                }

                if (StringUtil.isNotEmpty(oldEntity.getQlbdm())) {
                    newEntity.setStartingPlaceType(JSON.toJSONString(DicDataTurnUtil.PlaceTypeTurnMap(oldEntity.getQlbdm())));   //StartingPlaceType
                }

                if (StringUtil.isNotEmpty(oldEntity.getDwdm())) {
                    Map animalTypeMap = DicDataTurnUtil.animalTypeTurnMap(oldEntity.getDwdm());
                    String animalType = JSON.toJSONString(animalTypeMap);
                    newEntity.setAnimalType(animalType);                      //AnimalType
                }

                if (StringUtil.isNotEmpty(oldEntity.getSldw())) {
                    newEntity.setUnit(JSON.toJSONString(DicDataTurnUtil.UnitTurnMap(oldEntity.getSldw())));   //unit单位
                }

                JSONObject StartingCompany = new JSONObject();
                StartingCompany.put("UUID", StringUtil.isEmpty(oldEntity.getQqydm()) ? "" : oldEntity.getQqydm());
                StartingCompany.put("Name", oldEntity.getQdmc());
                newEntity.setStartingPlaceCompany(StartingCompany.toJSONString());      //StartingPlaceCompany



                JSONObject desCompany = new JSONObject();
                desCompany.put("UUID", "");
                desCompany.put("Name", oldEntity.getDdmc());
                if(oldEntity.getDytdm().equals("01")) {
                    TXDRSlaughterHouse tXDRSlaughterHouse = txdrSlaughterHouseService.getSlaughterByName(oldEntity.getDdmc());
                    if (tXDRSlaughterHouse != null) {
                        desCompany.put("UUID", tXDRSlaughterHouse.getMid());
                        desCompany.put("Name", tXDRSlaughterHouse.getDisplayName());
                    }
                }
                newEntity.setDestinationPlaceCompany(desCompany.toJSONString());          //DestinationPlaceCompany

                newEntity.setCreator_id("9f4cb3017b314889a230f6140543e423");
                newEntity.setModifier_id("9f4cb3017b314889a230f6140543e423");
                newEntity.setPartition_id("0b1d178c499043a2aeeef591a3d8f03d");
                newEntity.setPartition_ids("{0b1d178c499043a2aeeef591a3d8f03d,d5896b31964e425382df52f655dedfc2}");
                newEntity.setCategory_id("7f346e9b82414eb2bf1f3126906abe29");
                newEntity.setDisabled("f");
                newEntity.setDeleted("f");
                newEntity.setApp_id("4dcb17fbbcc44f19b724efd9d7275302");

                JSONObject _CheckResult = new JSONObject();
                _CheckResult.put("key", 2803);
                _CheckResult.put("Name", "未检查");
                newEntity.setCheckResult(_CheckResult.toJSONString());   //CheckResult

                String jyzzl = oldEntity.getJyzzl(); //检疫证种类、检疫证链接
                if (!StringUtils.isEmpty(jyzzl) && jyzzl.equals("1")) {  //1电子证  2纸质证
                    JSONObject certSource = new JSONObject();
                    certSource.put("key", 3602);
                    certSource.put("Name", "电子证");
                    newEntity.setCertSource(certSource.toJSONString());
                } else {
                    JSONObject certSource = new JSONObject();
                    certSource.put("key", 3601);
                    certSource.put("Name", "纸质证");
                    newEntity.setCertSource(certSource.toJSONString());
                }
                newEntity.setJyzlj(oldEntity.getJyzlj());  //检疫证链接
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return newEntity;
    }


}
